/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package parser.part.n2;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Patronus
 */
public class XMLparser {

    public List parce(String name) throws ParserConfigurationException, SAXException, IOException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        Params p = new Params();
        Creator te = new Creator();

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        //Using factory get an instance of document builder
        //parse using builder to get DOM representation of the XML file
        p.dom = db.parse("D:\\out.xml");
        Element docEle = p.dom.getDocumentElement();
        NodeList elements = docEle.getElementsByTagName("rule");

        for (int i = 0; i < elements.getLength(); i++) {
            p.oneitem = (Element) elements.item(i);
            p.atributes = p.oneitem.getAttributes();

            for (int atr = 0; atr < p.atributes.getLength(); atr++) {
                p.line = Integer.valueOf(p.oneitem.getAttribute("line"));
                p.param = p.oneitem.getElementsByTagName("return");
                p.returntype = p.param.item(0).getTextContent();
                p.param = p.oneitem.getElementsByTagName("classname");
                p.classname = p.param.item(0).getTextContent();
                p.param = p.oneitem.getElementsByTagName("expression");
                p.expression = p.param.item(0).getTextContent();
                p.param = p.oneitem.getElementsByTagName("id");
                p.id = p.param.item(0).getTextContent();
                p.param = p.oneitem.getElementsByTagName("arguments");

                Element r = (Element) p.param.item(0);
                p.args = (NodeList) r.getElementsByTagName("args");
                Object[][] argsarrray = new Object[p.args.getLength()][2];
                for (int b = 0; b < p.args.getLength(); b++) {
                    Element thisrlrment = (Element) p.args.item(b);
                    argsarrray[b][0] = thisrlrment.getAttribute("position");
                    argsarrray[b][1] = thisrlrment.getAttribute("value");
                }

                p.param = p.oneitem.getElementsByTagName("importParametrs");

                r = (Element) p.param.item(0);
                p.args = (NodeList) r.getElementsByTagName("import");
                Object[][] arrayforimport = new Object[p.args.getLength()][2];
                for (int b = 0; b < p.args.getLength(); b++) {
                    Element impsarrray = (Element) p.args.item(b);
                    arrayforimport[b][0] = impsarrray.getAttribute("position");
                    arrayforimport[b][1] = impsarrray.getAttribute("value");
                }



                Ruler newrule = createRule(p.classname, p.id, argsarrray, arrayforimport, p.returntype);
                te.addRule(p.line, newrule);

            }



        }

        return te.getTable();
    }

    private Ruler createRule(String classname, String id, Object[][] args, Object[][] imp, String returntype) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

        try {
            Class rule = Class.forName(classname);
            Object r = rule.newInstance();
            Ruler ruler = (Ruler) r;


            ruler.setId(id);
//             блоки загрузки параметров
            for (int i = 0; i < args.length; i++) {
                ruler.setArgs(
                        Integer.valueOf(String.valueOf(args[i][0])),
                        args[i][1]);
            }

            for (int i = 0; i < imp.length; i++) {
                ruler.setParameterImport(Integer.valueOf(String.valueOf(imp[i][0])), String.valueOf(imp[i][1]));
            }
            ruler.setReturnResult(Boolean.parseBoolean(returntype));
            return ruler;



        } catch (InstantiationException ex) {
            Logger.getLogger(XMLparser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(XMLparser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(XMLparser.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}

class Params {

    Document dom;
    NodeList args;
    Element oneitem;
    String returntype;
    Element e;
    NamedNodeMap atributes;
    NodeList param;
    int line;
    String classname;
    String id;
    String expression;
}